/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.company.management.controller;

import java.io.Serializable;

import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.company.management.entity.Schedule;
import org.company.management.entity.Employee;
import org.company.management.entity.EmployeeScheduleInfo;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.company.management.entity.EmployeeSchedule;
import org.company.management.controller.exceptions.IllegalOrphanException;
import org.company.management.controller.exceptions.NonexistentEntityException;

/**
 *
 * @author Paner
 */
@SuppressWarnings("serial")
public class EmployeeScheduleJpaController implements Serializable {

    public EmployeeScheduleJpaController() {
    	emf = Persistence.createEntityManagerFactory("Management");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EmployeeSchedule employeeSchedule) {
        if (employeeSchedule.getEmployeeScheduleInfoList() == null) {
            employeeSchedule.setEmployeeScheduleInfoList(new ArrayList<EmployeeScheduleInfo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Schedule scheduleid = employeeSchedule.getScheduleid();
            if (scheduleid != null) {
                scheduleid = em.getReference(scheduleid.getClass(), scheduleid.getId());
                employeeSchedule.setScheduleid(scheduleid);
            }
            Employee employeeid = employeeSchedule.getEmployeeid();
            if (employeeid != null) {
                employeeid = em.getReference(employeeid.getClass(), employeeid.getId());
                employeeSchedule.setEmployeeid(employeeid);
            }
            List<EmployeeScheduleInfo> attachedEmployeeScheduleInfoList = new ArrayList<EmployeeScheduleInfo>();
            for (EmployeeScheduleInfo employeeScheduleInfoListEmployeeScheduleInfoToAttach : employeeSchedule.getEmployeeScheduleInfoList()) {
                employeeScheduleInfoListEmployeeScheduleInfoToAttach = em.getReference(employeeScheduleInfoListEmployeeScheduleInfoToAttach.getClass(), employeeScheduleInfoListEmployeeScheduleInfoToAttach.getId());
                attachedEmployeeScheduleInfoList.add(employeeScheduleInfoListEmployeeScheduleInfoToAttach);
            }
            employeeSchedule.setEmployeeScheduleInfoList(attachedEmployeeScheduleInfoList);
            em.persist(employeeSchedule);
            if (scheduleid != null) {
                scheduleid.getEmployeeScheduleList().add(employeeSchedule);
                scheduleid = em.merge(scheduleid);
            }
            if (employeeid != null) {
                employeeid.getEmployeeScheduleList().add(employeeSchedule);
                employeeid = em.merge(employeeid);
            }
            for (EmployeeScheduleInfo employeeScheduleInfoListEmployeeScheduleInfo : employeeSchedule.getEmployeeScheduleInfoList()) {
                EmployeeSchedule oldEmployeescheduleidOfEmployeeScheduleInfoListEmployeeScheduleInfo = employeeScheduleInfoListEmployeeScheduleInfo.getEmployeescheduleid();
                employeeScheduleInfoListEmployeeScheduleInfo.setEmployeescheduleid(employeeSchedule);
                employeeScheduleInfoListEmployeeScheduleInfo = em.merge(employeeScheduleInfoListEmployeeScheduleInfo);
                if (oldEmployeescheduleidOfEmployeeScheduleInfoListEmployeeScheduleInfo != null) {
                    oldEmployeescheduleidOfEmployeeScheduleInfoListEmployeeScheduleInfo.getEmployeeScheduleInfoList().remove(employeeScheduleInfoListEmployeeScheduleInfo);
                    oldEmployeescheduleidOfEmployeeScheduleInfoListEmployeeScheduleInfo = em.merge(oldEmployeescheduleidOfEmployeeScheduleInfoListEmployeeScheduleInfo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EmployeeSchedule employeeSchedule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeSchedule persistentEmployeeSchedule = em.find(EmployeeSchedule.class, employeeSchedule.getId());
            Schedule scheduleidOld = persistentEmployeeSchedule.getScheduleid();
            Schedule scheduleidNew = employeeSchedule.getScheduleid();
            Employee employeeidOld = persistentEmployeeSchedule.getEmployeeid();
            Employee employeeidNew = employeeSchedule.getEmployeeid();
            List<EmployeeScheduleInfo> employeeScheduleInfoListOld = persistentEmployeeSchedule.getEmployeeScheduleInfoList();
            List<EmployeeScheduleInfo> employeeScheduleInfoListNew = employeeSchedule.getEmployeeScheduleInfoList();
            List<String> illegalOrphanMessages = null;
            for (EmployeeScheduleInfo employeeScheduleInfoListOldEmployeeScheduleInfo : employeeScheduleInfoListOld) {
                if (!employeeScheduleInfoListNew.contains(employeeScheduleInfoListOldEmployeeScheduleInfo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EmployeeScheduleInfo " + employeeScheduleInfoListOldEmployeeScheduleInfo + " since its employeescheduleid field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (scheduleidNew != null) {
                scheduleidNew = em.getReference(scheduleidNew.getClass(), scheduleidNew.getId());
                employeeSchedule.setScheduleid(scheduleidNew);
            }
            if (employeeidNew != null) {
                employeeidNew = em.getReference(employeeidNew.getClass(), employeeidNew.getId());
                employeeSchedule.setEmployeeid(employeeidNew);
            }
            List<EmployeeScheduleInfo> attachedEmployeeScheduleInfoListNew = new ArrayList<EmployeeScheduleInfo>();
            for (EmployeeScheduleInfo employeeScheduleInfoListNewEmployeeScheduleInfoToAttach : employeeScheduleInfoListNew) {
                employeeScheduleInfoListNewEmployeeScheduleInfoToAttach = em.getReference(employeeScheduleInfoListNewEmployeeScheduleInfoToAttach.getClass(), employeeScheduleInfoListNewEmployeeScheduleInfoToAttach.getId());
                attachedEmployeeScheduleInfoListNew.add(employeeScheduleInfoListNewEmployeeScheduleInfoToAttach);
            }
            employeeScheduleInfoListNew = attachedEmployeeScheduleInfoListNew;
            employeeSchedule.setEmployeeScheduleInfoList(employeeScheduleInfoListNew);
            employeeSchedule = em.merge(employeeSchedule);
            if (scheduleidOld != null && !scheduleidOld.equals(scheduleidNew)) {
                scheduleidOld.getEmployeeScheduleList().remove(employeeSchedule);
                scheduleidOld = em.merge(scheduleidOld);
            }
            if (scheduleidNew != null && !scheduleidNew.equals(scheduleidOld)) {
                scheduleidNew.getEmployeeScheduleList().add(employeeSchedule);
                scheduleidNew = em.merge(scheduleidNew);
            }
            if (employeeidOld != null && !employeeidOld.equals(employeeidNew)) {
                employeeidOld.getEmployeeScheduleList().remove(employeeSchedule);
                employeeidOld = em.merge(employeeidOld);
            }
            if (employeeidNew != null && !employeeidNew.equals(employeeidOld)) {
                employeeidNew.getEmployeeScheduleList().add(employeeSchedule);
                employeeidNew = em.merge(employeeidNew);
            }
            for (EmployeeScheduleInfo employeeScheduleInfoListNewEmployeeScheduleInfo : employeeScheduleInfoListNew) {
                if (!employeeScheduleInfoListOld.contains(employeeScheduleInfoListNewEmployeeScheduleInfo)) {
                    EmployeeSchedule oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo = employeeScheduleInfoListNewEmployeeScheduleInfo.getEmployeescheduleid();
                    employeeScheduleInfoListNewEmployeeScheduleInfo.setEmployeescheduleid(employeeSchedule);
                    employeeScheduleInfoListNewEmployeeScheduleInfo = em.merge(employeeScheduleInfoListNewEmployeeScheduleInfo);
                    if (oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo != null && !oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo.equals(employeeSchedule)) {
                        oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo.getEmployeeScheduleInfoList().remove(employeeScheduleInfoListNewEmployeeScheduleInfo);
                        oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo = em.merge(oldEmployeescheduleidOfEmployeeScheduleInfoListNewEmployeeScheduleInfo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = employeeSchedule.getId();
                if (findEmployeeSchedule(id) == null) {
                    throw new NonexistentEntityException("The employeeSchedule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeSchedule employeeSchedule;
            try {
                employeeSchedule = em.getReference(EmployeeSchedule.class, id);
                employeeSchedule.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employeeSchedule with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<EmployeeScheduleInfo> employeeScheduleInfoListOrphanCheck = employeeSchedule.getEmployeeScheduleInfoList();
            for (EmployeeScheduleInfo employeeScheduleInfoListOrphanCheckEmployeeScheduleInfo : employeeScheduleInfoListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This EmployeeSchedule (" + employeeSchedule + ") cannot be destroyed since the EmployeeScheduleInfo " + employeeScheduleInfoListOrphanCheckEmployeeScheduleInfo + " in its employeeScheduleInfoList field has a non-nullable employeescheduleid field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Schedule scheduleid = employeeSchedule.getScheduleid();
            if (scheduleid != null) {
                scheduleid.getEmployeeScheduleList().remove(employeeSchedule);
                scheduleid = em.merge(scheduleid);
            }
            Employee employeeid = employeeSchedule.getEmployeeid();
            if (employeeid != null) {
                employeeid.getEmployeeScheduleList().remove(employeeSchedule);
                employeeid = em.merge(employeeid);
            }
            em.remove(employeeSchedule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EmployeeSchedule> findEmployeeScheduleEntities() {
        return findEmployeeScheduleEntities(true, -1, -1);
    }

    public List<EmployeeSchedule> findEmployeeScheduleEntities(int maxResults, int firstResult) {
        return findEmployeeScheduleEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<EmployeeSchedule> findEmployeeScheduleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(EmployeeSchedule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EmployeeSchedule findEmployeeSchedule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EmployeeSchedule.class, id);
        } finally {
            em.close();
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public int getEmployeeScheduleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<EmployeeSchedule> rt = cq.from(EmployeeSchedule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
